package com.sixth.oa.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.sixth.oa.entity.admin.sixthAdmin;
import com.sixth.oa.entity.model.sixthMenu;
import com.sixth.oa.mapper.sixthAdminMapper;
import com.sixth.oa.mapper.sixthMenuMapper;
import com.sixth.oa.util.ResultStr;

@RequestMapping("/admin")
@Controller
public class AdminController {

	@Autowired
	sixthAdminMapper adminMapper;
	@Autowired
	sixthMenuMapper menuMapper;
	
	
	
	/**
	 * 管理员添加
	 * @param admin
	 * @return
	 */
	@PostMapping("/addAdmin")
	@ResponseBody
	public ResultStr addAdminUser(sixthAdmin admin) {
		int i = adminMapper.insert(admin);
		if(i>0) {
			return new ResultStr(200,"注册成功",null);
		}else {
			return new ResultStr(400,"网络错误请重试",null);
		}
	}
	
	/**
	 * 添加菜单
	 * @param menu
	 * @return
	 */
	@PostMapping("/addMenu")
	@ResponseBody
	public ResultStr addMenu(sixthMenu menu) {
		int i =menuMapper.insert(menu);
		if(i>0) {
			return new ResultStr(200,"添加成功",null);
		}else {
			return new ResultStr(400,"添加失败",null);
		}
	}
	
	
	/**
	 * 查询全部菜单
	 * @return
	 */
	@GetMapping("/getMenu")
	@ResponseBody
	public ResultStr selMenu() {
		
		ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
		 
		List<sixthMenu> listP = menuMapper.selectAllMenu(0);
		if(null!=listP) {
			for(int i =0;i<listP.size();i++) {
				HashMap<String,Object> map = new HashMap<String,Object>();
				map.put("text", listP.get(i).getText());
				map.put("icon", listP.get(i).getIcon());
				if(listP.get(i).getTarget()==1) {
				map.put("target", listP.get(i).getTarget());
				}
				List<sixthMenu> listS = menuMapper.selectAllMenu(listP.get(i).getId());
				if(null!=listS&&listS.size()>0) {
				map.put("subset", listS);
				}else {
					map.put("href", listP.get(i).getHref());	
				}
				list.add(map);
			}
			String json = JSON.toJSONString(list);
			System.out.println("封装后的json:"+json);
			return new ResultStr(200,"sucess",list);
		}else {
			return new ResultStr(400,"fail",null);
		}
	}
	
	
}
